﻿#region MyRegion
//using Microsoft.AspNetCore.Mvc;
//using ApiManage.Svc.Filter;
//using ApiManage.Models.Options;
//using ApiManage.Models.zyhModels;
//using ApiManage.Service.IService;
//using ApiManage.Service.Service;
//using Rhonin.Common;
//using Rhonin.Common.Entity.Result;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Linq.Expressions;

//namespace ApiManage.Svc.Controllers
//{
//    /// <summary>
//    /// 选择项控制器
//    /// </summary>
//    [Route("api/[controller]")]
//    [ApiController]
//    public class OptionController : BaseController
//    {
//        readonly ISysRoleService _sysRoleService = new SysRoleService();
//        readonly IWorkProjectService _projectService = new WorkProjectService();
//        readonly IWorkModuleService _workModuleService = new WorkModuleService();
//        /// <summary>
//        /// 获取角色选择项
//        /// </summary>
//        /// <returns></returns>
//        [Route("RoleOption"), HttpGet, CheckLogin]
//        public Result<List<RoleOption>> RoleOption()
//        {
//            Expression<Func<SysRole, bool>> where = o => o.Mark != 9 && o.RoleId != 0;
//            var roles = _sysRoleService.GetRole(where);
//            var options = roles.Select(o => new RoleOption
//            {
//                Id = o.RoleId,
//                Name = o.RoleName
//            }).ToList();
//            options.Insert(0, new RoleOption
//            {
//                Id = 0,
//                Name = "请选择角色"
//            });
//            return ApiResult<List<RoleOption>>.TResult(options);
//        }
//        /// <summary>
//        /// 获取项目选择项
//        /// </summary>
//        /// <returns></returns>
//        [Route("ProjectOption"), HttpGet, CheckLogin]
//        public Result<List<ProjectOption>> ProjectOption()
//        {
//            Expression<Func<WorkProject, bool>> where = o => o.Status != 9;
//            var roles = _projectService.GetList(where);
//            var options = roles.Select(o => new ProjectOption
//            {
//                Id = o.Id,
//                Name = o.Name
//            }).ToList();
//            options.Insert(0, new ProjectOption
//            {
//                Id = 0,
//                Name = "请选择项目"
//            });
//            return ApiResult<List<ProjectOption>>.TResult(options);
//        }
//        /// <summary>
//        /// 获取项目选择项
//        /// </summary>
//        /// <returns></returns>
//        [Route("ModuleOption"), HttpGet, CheckLogin]
//        public Result<List<ModuleOption>> ModuleOption()
//        {
//            Expression<Func<WorkProject, bool>> where = o => o.Status != 9;
//            var roles = _projectService.GetList(where);
//            var options = roles.Select(o => new ModuleOption
//            {
//                Id = o.Id,
//                Name = o.Name
//            }).ToList();
//            foreach (var item in options)
//            {
//                item.children = _workModuleService.GetList(o => o.ProjectId == item.Id && o.Status != 9).Select(o => new ModuleOption
//                {
//                    Id = o.Id,
//                    Name = o.Name
//                }).ToList();
//            }
//            options.Insert(0, new ModuleOption
//            {
//                Id = 0,
//                Name = "请选择所属"
//            });
//            return ApiResult<List<ModuleOption>>.TResult(options);
//        }

//    }
//}
#endregion

#region MyRegion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ApiManage.Models.MongoMappings.Project;
using ApiManage.Models.MongoMappings.Sys;
using ApiManage.Models.Options;
using ApiManage.Service.Mongo.Project;
using ApiManage.Service.Mongo.Sys;
using ApiManage.Svc.Filter;
using Microsoft.AspNetCore.Mvc;
using Rhonin.Common.Entity.Api;

namespace ApiManage.Svc.Controllers
{
    //    /// <summary>
    //    /// 选择项控制器
    //    /// </summary>
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class OptionController : BaseController
    {
        private readonly ISysRoleQueryService _sysRoleQueryService;
        private readonly ISysUserQueryService _sysUserQueryService;
        private readonly IProjectQueryService _projectQueryService;
        private readonly IModulesQueryService _modulesQueryService;
        /// <summary>
        /// 
        /// </summary>
        public OptionController(ISysRoleQueryService sysRoleQueryService,
            ISysUserQueryService sysUserQueryService,
            IModulesQueryService modulesQueryService,
            IProjectQueryService projectQueryService)
        {
            _sysRoleQueryService = sysRoleQueryService;
            _sysUserQueryService = sysUserQueryService;
            _projectQueryService = projectQueryService;
            _modulesQueryService = modulesQueryService;
        }
        /// <summary>
        /// 获取角色选择项
        /// </summary>
        /// <returns></returns>
        [Route("UserOption"), HttpGet, CheckLogin]
        public ApiResult<List<UserOption>> UserOption()
        {
            Expression<Func<SysUser, bool>> where = o => o.Status != "Delete";
            var roles = _sysUserQueryService.GetList(where);
            var options = roles.Select(o => new UserOption
            {
                Id = o.Id.ToString(),
                Name = o.UserName
            }).ToList();
            options.Insert(0, new UserOption
            {
                Id = "",
                Name = "请选择用户"
            });
            return CreateResult(options);
        }
        /// <summary>
        /// 获取角色选择项
        /// </summary>
        /// <returns></returns>
        [Route("RoleOption"), HttpGet, CheckLogin]
        public ApiResult<List<RoleOption>> RoleOption()
        {
            Expression<Func<SysRole, bool>> where = o => o.Status != "Delete";
            var roles = _sysRoleQueryService.GetList(where);
            var options = roles.Select(o => new RoleOption
            {
                Id = o.Id.ToString(),
                Name = o.RoleName
            }).ToList();
            options.Insert(0, new RoleOption
            {
                Id = "",
                Name = "请选择角色"
            });
            return CreateResult(options);
        }
        /// <summary>
        /// 获取项目选择项
        /// </summary>
        /// <returns></returns>
        [Route("ProjectOption"), HttpGet, CheckLogin]
        public ApiResult<List<ProjectOption>> ProjectOption()
        {
            Expression<Func<Projects, bool>> where = o => o.Status != "Delete";
            var roles = _projectQueryService.GetList(where);
            var options = roles.Select(o => new ProjectOption
            {
                Id = o.Id.ToString(),
                Name = o.Name
            }).ToList();
            options.Insert(0, new ProjectOption
            {
                Id = "",
                Name = "请选择项目"
            });
            return CreateResult(options);
        }
        /// <summary>
        /// 获取项目选择项
        /// </summary>
        /// <returns></returns>
        [Route("ModuleOption"), HttpGet, CheckLogin]
        public ApiResult<List<ModuleOption>> ModuleOption()
        {
            Expression<Func<Projects, bool>> where = o => o.Status != "Delete";
            var roles = _projectQueryService.GetList(where);
            var options = roles.Select(o => new ModuleOption
            {
                Id = o.Id.ToString(),
                Name = o.Name,
                Url = !string.IsNullOrEmpty(o.Url) ? o.Url : "",
                Host = o.Host
            }).ToList();
            foreach (var item in options)
            {
                item.Children = _modulesQueryService.GetList(o => o.ProjectId == item.Id && o.Status != "Delete").Select(o => new ModuleOption
                {
                    Id = o.Id.ToString(),
                    Name = o.Name,
                    Url = item.Url + "/" + (!string.IsNullOrEmpty(o.Path) ? o.Path : "")
                }).ToList();
            }
            options.Insert(0, new ModuleOption
            {
                Id = "",
                Name = "请选择所属"
            });
            return CreateResult(options);
        }
    }

}

#endregion